package com.dream.service.user.web.processor.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.service.user.api.UserAddrService;
import com.dream.service.user.domain.model.UserAddrModel;
import com.dream.service.user.web.model.FieldSchema;
import com.dream.service.user.web.model.Param;
import com.dream.service.user.web.processor.RequestProcessor;
import com.dream.service.user.web.processor.exception.ParamException;
import com.dream.service.user.web.processor.param.ParamProcessor;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kangd on 2016/3/24.
 */
class UserAddrRequestProcessor implements RequestProcessor {

    UserAddrService userAddrService;

    public UserAddrService getUserAddrService() {
        return userAddrService;
    }

    public void setUserAddrService(UserAddrService userAddrService) {
        this.userAddrService = userAddrService;
    }

    @Override
    public JSONObject processor(HttpServletRequest req) {
        JSONObject result = new JSONObject();
        String cmd = req.getParameter(Param.CMD);
        try {
            if (cmd.equals(Param.UserAddrCMD.USDERADDR_LIST)) {
                this.getUserAddrList(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_DEL)) {
                this.delUserAddress(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_MODFIY_DEFAULT)) {
                this.modifyUserDefaultAddr(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_GET_DEFAULT)) {
                this.getUserDefaultAddress(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_GET_ADDR)) {
                this.getUserAddress(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_Add)) {
                this.addUserAddress(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_MODFIY)) {
                this.modifyUserAddress(req, result);
            }else if (cmd.equals(Param.UserAddrCMD.USDERADDR_COUNTRY)) {
                this.getCountry(req, result);
            }else {
                AppResultBuilder.buildCmdErrorResult(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildResult(result,"",false, ErrorCode.UNKOWN);
        }
        return result;
    }

    //地址列表
    // http://motif.evermarker.com/useraddr?cmd=list&pin=kdd
    public void getUserAddrList(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        if(StringUtils.isEmpty(pin)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null","");
            return;
        }
        BaseListResponse baseListResponse = null;
        try {
            baseListResponse = userAddrService.getUserAddressList(pin);
            if (baseListResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseListResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseListResponse.getError_code(), baseListResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }

    }

    //删除地址
    // http://motif.evermarker.com/useraddr?cmd=del&pin=kdd&aid=10002
    public void  delUserAddress(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        String userAddressIDStr =  req.getParameter(FieldSchema.USER_ADDR_ID);
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(userAddressIDStr)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null or userAddressID wrong","");
            return;
        }
        int userAddressID = Integer.valueOf(userAddressIDStr);
        BaseResponse<Boolean> baseResponse = null;
        try {
            baseResponse = userAddrService.delUserAddress(pin,userAddressID);
            if (baseResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,new Object());
            }else{
                if(baseResponse.getError_code()==ErrorCode.NOT_AUTH.getCode()){
                    AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(), baseResponse.getError_msg());
                }else {
                    AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(),"");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }

    //修改为默认地址
    // http://motif.evermarker.com/useraddr?cmd=mdefault&pin=kdd&aid=10002
    public void  modifyUserDefaultAddr(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        String userAddressIDStr =  req.getParameter(FieldSchema.USER_ADDR_ID);
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(userAddressIDStr)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null or userAddressID wrong","");
            return;
        }
        int userAddressID = Integer.valueOf(userAddressIDStr);
        BaseResponse<Boolean> baseResponse = null;
        try {
            baseResponse = userAddrService.modifyUserDefaultAddr(pin, userAddressID);
            if (baseResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,new Object());
            }else{
                AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }

    // 获取默认地址
    // http://motif.evermarker.com/useraddr?cmd=gdefault&pin=kdd&aid=1001
    public void  getUserAddress(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        String userAddressIDStr =  req.getParameter(FieldSchema.USER_ADDR_ID);
        if(StringUtils.isEmpty(pin) || StringUtils.isEmpty(userAddressIDStr)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null or userAddressID wrong","");
            return;
        }
        int userAddressID = Integer.valueOf(userAddressIDStr);
        BaseResponse<UserAddrModel> baseResponse = null;
        try {
            baseResponse = userAddrService.getUserAddress(pin,userAddressID);
            if (baseResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }



    // 获取默认地址
    // http://motif.evermarker.com/useraddr?cmd=getaddr&pin=kdd
    public void  getUserDefaultAddress(HttpServletRequest req,JSONObject result){
        String pin = req.getParameter(FieldSchema.PIN);
        if(StringUtils.isEmpty(pin)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null","");
            return;
        }
        BaseResponse<UserAddrModel> baseResponse = null;
        try {
            baseResponse = userAddrService.getUserDefaultAddress(pin);
            if (baseResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }

    //增加地址
    // http://motif.evermarker.com/useraddr?cmd=add&pin=kdd&email=kangdong@evermarker.net&tel=15810343777&name=kangdong&addr1=tuanjiehu&addr2=tuanjiegongyu&city=beijing&state=beijing&zip=100001&idnum=231313131313&country=china&isd=1

    public void  addUserAddress(HttpServletRequest req,JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        if(StringUtils.isEmpty(pin)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null","");
            return;
        }
        boolean debug = "true".equals(req.getParameter("debug"));

        UserAddrModel userAddrModel=null;
        try {
            userAddrModel = ParamProcessor.processorUserAddr(req);
        } catch (ParamException e) {
            e.printStackTrace();
            AppResultBuilder.buildParamErrorResult(result,e.getMessage(),"");
        }
        try {
            if(debug){
                result.put("request_param",JSONObject.toJSONString(userAddrModel));
            }
            BaseResponse<UserAddrModel> baseResponse = userAddrService.addUserAddress(userAddrModel);
            if (baseResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), "Service Error", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }

    //修改地址
    // http://motif.evermarker.com/useraddr?cmd=modify&pin=kdd&aid=10002&email=kangdong@evermarker.net&tel=15810343777&name=kangdong&addr1=tuanjiehu&addr2=tuanjiegongyu&city=beijing&state=beijing&zip=100001&idnum=231313131313&country=china&isd=1
    public void  modifyUserAddress(HttpServletRequest req,JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        if(StringUtils.isEmpty(pin)){
            AppResultBuilder.buildParamErrorResult(result,"pin is null","");
            return;
        }
        UserAddrModel userAddrModel=null;
        try {
            userAddrModel = ParamProcessor.processorUserAddr(req);
            if(userAddrModel.getReceiving_id() == null){
                AppResultBuilder.buildParamErrorResult(result,"pin is null","");
                return;
            }
        } catch (ParamException e) {
            e.printStackTrace();
            AppResultBuilder.buildParamErrorResult(result,e.getMessage(),"");
        }

        try {
            BaseResponse<UserAddrModel> baseResponse = userAddrService.modifyUserAddress(userAddrModel);
            if (baseResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result,baseResponse.getResult());
            }else{
                AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), "Service Error", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }


    //国家
    // http://motif.evermarker.com/useraddr?cmd=country
    public void  getCountry(HttpServletRequest req,JSONObject result) {
        BaseListResponse baseListResponse = userAddrService.getCountry();
        if (baseListResponse.isSuccess()) {
            AppResultBuilder.buildSucResult(result,baseListResponse.getResult());
        }
    }
}
